package cloud.zhixinglu.cloud.spring.boot.websocket.controller;

import cn.hutool.core.util.IdUtil;
import com.github.fanpan26.TioWebSocketServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;

import java.util.Objects;

/**
 * @author Administrator
 */
@Slf4j
@RequestMapping("/socket/")
@RestController
public class SendController {

    @Autowired
    private TioWebSocketServerBootstrap bootstrap;

    @GetMapping("send")
    public void send(String message){
        if(Objects.equals(message, null) ){
            log.error("message is null");
            return;
        }
        //String uniqueId = IdUtil.getSnowflakeNextIdStr();
        Tio.sendToUser(bootstrap.getServerTioConfig(), "1", WsResponse.fromText(message,"utf-8"));
        Tio.sendToAll(bootstrap.getServerTioConfig(), WsResponse.fromText(message,"utf-8"));
    }

}
